package com.gkfreshmart.cloud.qo;

import lombok.Getter;
import lombok.Setter;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.query.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 查询请求体超类
 *
 * @param <T>
 * @author Bruin
 */
@Setter
@Getter
public class RequestCondition<T, C> {
    private final Class<T> clazz;
    @Autowired
    private SQLManager sqlManager;

    private T entity;
    private C condition;

    public RequestCondition(C condition, Class<T> clazz) {
        this.clazz = clazz;
        this.condition = condition;
        try {
            entity = clazz.newInstance();
            BeanUtils.copyProperties(this.condition, this.entity);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public Query<T> getQuery() {
        return new Query<>(sqlManager, clazz);
    }
}
